<template>
  <div class="m-spec-contain">
    <!-- 商品规格列表 -->
    <div class="add-btn">
      <w-button type="option" @on-click="$router.push(`addspecifications?three=${$route.query.three}`)">新增</w-button>
    </div>
    <div class="spec-table">
      <w-table :columns="columns" :has-data="hasData">
        <tr v-for="(list, index) in listParams.recordList" :key="index">
          <td>
            <w-checkbox :label="`list${index}`" v-model="list.checked" @on-change="_setListToAllCheck"></w-checkbox>
          </td>
          <td>
            <div class="m-order">
              <span class="order">{{list.sequence}}</span>
            </div>
          </td>
          <td>
            <span>{{list.glist}}</span>
          </td>
          <td>
            <div class="options">
              <div class="edit" @click="$router.push(`addspecifications?three=${$route.query.three}&id=${list.id}`)">
                <span class="icon-edit"></span>
                <span>修改</span>
              </div>
              <div class="del" @click="onDelSpec(list.id)">
                <span class="icon-del"></span>
                <span>删除</span>
              </div>
            </div>
          </td>
        </tr>
      </w-table>
    </div>

    <!-- 全选，分页 -->
    <div class="m-all-page clearfix" v-if="listParams.recordList && listParams.recordList.length">
      <div class="fl" style="margin-top: 10px;">
        <w-checkbox name="全选" :label="`all`" v-model="all" @on-change="onChangeCheckedAll">全选</w-checkbox>
        <span class="delete" @click="onDelSpec('')">删除</span>
      </div>
      <div class="fr" v-if="paramsInfo.pageCount > 1">
        <w-paging :current="paramsInfo.currentPage" :total="paramsInfo.pageCount"></w-paging>
      </div>
    </div>

  </div>
</template>

<script>
import { apiClassifyMange } from 'js/api'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      columns: [
        {title: '', width: '4%'},
        {title: '序号', width: '6%'},
        {title: '商品规格', width: 'auto'},
        {title: '操作', width: '24%'}
      ],
      all: false, // 全选
      paramsInfo: {
        accessToken: '',
        threeMenuCode: this.$route.query.three || '',
        currentPage: 1,
        showCount: 20
      },
      listParams: {}
    }
  },
  computed: {
    ...mapGetters(['token']),
    // 判断是否有数据
    hasData() {
      if (!this.listParams.recordList || this.listParams.recordList.length == 0) {
        return false
      } else {
        return true
      }
    }
  },
  created() {
    this.fetchSepcsList()
  },
  methods: {
    /**
     * 获取商品规格查询列表
     */
    fetchSepcsList() {
      let _this = this
      let spec = ''
      this.paramsInfo.accessToken = this.token
      apiClassifyMange.apiGoodsSpecs(this.paramsInfo).then(data => {
        this.listParams = data.data || {}
        if (this.listParams.recordList && this.listParams.recordList.length) {
          this.listParams.recordList.forEach((item, index) => {
            let arr = []
            item.glist.forEach(v => {
              arr.push(v.value)
              spec = arr.join(',')
            })
            _this.$set(_this.listParams.recordList[index], 'glist', spec)
          })
        }
      })
    },
    /**
     * 切换全选
     */
    onChangeCheckedAll(bool) {
      if (bool && this.listParams.recordList && this.listParams.recordList.length) {
        this.listParams.recordList.forEach(list => {
          list.checked = true
        })
      } else {
        this.listParams.recordList.forEach(list => {
          list.checked = false
        })
      }
    },
    /**
     * 每个CheckBox选中状态操作
     */
    _setListToAllCheck() {
      this.all = true
      // 如果商品列表中只要有一个是未选中状态，则全选按钮也为未选中状态
      this.listParams.recordList.forEach((v) => {
        if (!v.checked) {
          this.all = false
        }
      })
    },
    /**
     * 删除
     */
    onDelSpec(id) {
      let _this = this
      const ids = []
      if (id) {
        ids.push(id)
      }
      this.$modal({
        title: '提示',
        content: '确认删除？',
        onConfirm() {
          apiClassifyMange.apiGoodsSpecsDel(_this.token, _this.paramsInfo.threeMenuCode, ids).then(data => {
            _this.$toast('删除成功！', () => {
              _this.fetchSepcsList()
            })
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.m-spec-contain {
  .add-btn {
    .btn {
      width: 70px;
      height: 28px;
      border: 1px solid #9E9E9E;
      font-size: 12px;
    }
  }
  .spec-table {
    .table {
      td {
        height: 50px;
      }
    }
  }
}
</style>


<style lang="scss" scoped>
.add-btn {
  margin: 20px 0 10px;
}
.options {
  display: inline-block;
  text-align: center;
  color: #2D8EFE;
  & > div {
    display: inline-block;
    margin: 0 20px;
    cursor: pointer;
  }
  
  .icon-edit {
    width: 12px;
    height: 12px;
    vertical-align: sub;
    background: url(../../assets/icon-edit.png) no-repeat;
    display: inline-block;
    cursor: pointer;
  }
  .icon-del {
    display: inline-block;
    width: 12px;
    height: 12px;
    vertical-align: sub;
    background: url(../../assets/del-bush.png) no-repeat;
    cursor: pointer;
  }
}
.m-all-page {
  margin: 30px 0;
  padding-left: 10px;
  .delete {
    padding-left: 20px;
    text-decoration-line: underline;
    cursor: pointer;
  }
}
</style>
